package com.lindroy.androidplayer.ui.find.fragment

import android.os.Bundle
import android.view.View
import com.lindroy.androidplayer.base.BaseListFragment
import com.lindroy.androidplayer.databinding.FragmentSquareBinding
import com.lindroy.androidplayer.ui.adapter.ArticleAdapter
import com.lindroy.androidplayer.ui.webview.WebViewFragment
import com.lindroy.androidplayer.utils.addSimpleDivider
import com.lindroy.androidplayer.utils.collectListWhenResumed
import com.lindroy.androidplayer.viewmodels.SquareViewModel

/**
 * @author Lin
 * @date 2022/1/23
 * @function 广场
 * @Description
 */
class SquareFragment : BaseListFragment<FragmentSquareBinding, SquareViewModel>() {

    private val adapter by lazy { ArticleAdapter(viewLifecycleOwner) }

    override fun FragmentSquareBinding.initView(savedInstanceState: Bundle?) {
        vm = mViewMode
        rvSquare.addSimpleDivider()
        rvSquare.adapter = adapter
    }

    override fun FragmentSquareBinding.initListener() {
        mViewMode.setLayoutStateFlow(adapter.loadStateFlow)
        mViewMode.squareArticles.collectListWhenResumed(viewLifecycleOwner){
            adapter.submitData(it)
        }
        mViewMode.articleItemClickLiveData.observe(viewLifecycleOwner){
            WebViewFragment.start(this@SquareFragment,it.id,it.link,it.title,it.collect)
        }
    }

    override fun onRefresh() {
        adapter.refresh()
    }

    override fun onRetry(view: View) {
        adapter.refresh()
    }

}